const path = require('path')
const _process = require('child_process')
let _branch = _process.execSync('git symbolic-ref  --short HEAD').toString()
process.env.VUE_APP_GIT_BRANCH = _branch
const webpack = require('webpack')
let _cdn_path = 'http://eleshop.xunmengvip.com/xmbulkycdn/'
//本地CDN javascript
let _jsCDN = [
  'js/lodash.min.js',
  'js/moment.min.js',
  'js/numeral.min.js',
  'js/echarts.min.js',
  'js/vue-router.global.prod.min.js',
  'js/vuex.global.prod.min.js',
  'js/antd-with-locales.min.js',
  'js/jsencrypt.min.js',
  'js/qr-code-styling.js',
].map((x) => _cdn_path + x)
//本地CDN styleSheet
let _cssCDN = ['css/antd.min.css'].map((x) => _cdn_path + x)

if (process.env.VUE_APP_MY_NODE_ENV == 'development') {
  _jsCDN.unshift(_cdn_path + 'js/vue.global.min.js')
} else {
  _jsCDN.unshift(_cdn_path + 'js/vue.global.prod.min.js')
}

module.exports = {
  chainWebpack: (config) => {
    config.plugin('html').tap((args) => {
      const cdn = {
        js: _jsCDN,
        css: _cssCDN,
      }
      args[0].cdn = cdn
      return args
    })

    //console.log(config)

    config.set('externals', {
      lodash: '_',
      vue: 'Vue',
      moment: 'moment',
      echarts: 'echarts',
      'ant-design-vue': 'antd',
      'vue-router': 'VueRouter',
      numeral: 'numeral',
      Vuex: 'vuex',
      jsencrypt: 'JSEncrypt',
      'qr-code-styling': 'QRCodeStyling',
    })
  },
  publicPath: '.',
  outputDir: process.env.VUE_APP_MY_NODE_ENV == 'TASK' ? 'dist' : 'final', //测试环境DIST 正式环境 final

  lintOnSave: false,
  css: {
    sourceMap: true,
  },
  productionSourceMap: false,

  configureWebpack: {
    entry: {},

    performance: {
      maxAssetSize: 3000000,
      maxEntrypointSize: 300000,
    },
    resolve: {
      alias: {
        '@': path.resolve(__dirname + '/src'),
        '@Page': path.resolve(__dirname + '/src/Page'),
        '@Components': path.resolve(__dirname + '/src/components'),
        '@Config': path.resolve(__dirname + '/src/config'),
        '@Hooks': path.resolve(__dirname + '/src/hooks'),
        '@Assets': path.resolve(__dirname + '/src/assets'),
        '@Routers': path.resolve(__dirname + '/src/routers'),
        '@Untils': path.resolve(__dirname + '/src/untils'),
        '@Socket': path.resolve(__dirname + '/src/socket'),
        '@Sound': path.resolve(__dirname + '/src/config/sound'),
        '@Store': path.resolve(__dirname + '/src/store'),
        '@ant-design/icons-vue$': path.resolve(__dirname + '/src/assets/icons'),
      },
    },

    plugins: [
      new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
      //  require('unplugin-vue-components/webpack')({
      /* options */
      // }),
    ],
  },
  devServer: {
    disableHostCheck: true,
  },
}
